package com.first.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.AclUser;
import com.first.common.vo.Result;
import com.first.system.service.UserService;
import com.first.system.vo.AddUserVo;
import com.first.system.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: finrsteducation-parent
 * @description: 登录验证
 * @author: 张玉森
 * @create: 2023-05-22 20:24
 **/
@RestController
@RequestMapping("system/user")
@Api(tags = "用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("updateUserPassword/{oldPassword}/{newPassword}")
    @ApiOperation(value = "修改密码")
    public Result updatePassword(HttpServletRequest request,
                                 @PathVariable String oldPassword,
                                 @PathVariable String newPassword){
        AclUser aclUser = getAclUser(request);
        String username = aclUser.getUsername();
        String updatePassword= userService.selectPassword(username,oldPassword);
        if (updatePassword.equals("密码错误")){
            return Result.fail(updatePassword);
        }
        Integer integer = userService.updatePassword(aclUser, newPassword);
        if (integer>0){
            return Result.success();
        }
       return Result.fail("密码修改失败");
    }

    @DeleteMapping("deleteUserById/{id}")
    @ApiOperation(value = "删除用户")
    public Result deletedById(@PathVariable String id){
        Integer deleted= userService.deletedById(id);
        if (deleted>0){
            return Result.success();
        }
        return Result.fail();
    }
    @PostMapping("/updateUserStatusByid/{id}/{deleted}")
    @ApiOperation(value = "状态修改")
    public Result status(@PathVariable String id,@PathVariable Integer deleted){
        Integer num= userService.updateStatus(id,deleted);
        if (num>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    @PostMapping("/getUserByPage/{currentPage}/{pageSize}")
    @ApiOperation(value = "分页+条件查询")
    public Result list(@PathVariable Integer currentPage, @PathVariable Integer pageSize,@RequestBody  UserVo userVo) {
        //System.out.println(userVo);
        Page<AclUser> userPage = userService.findByPage(currentPage, pageSize, userVo);

        return Result.success(userPage);

    }
    @GetMapping("info")
    @ApiOperation("角色信息接口")
    public Result selectinfo(HttpServletRequest request) {
        AclUser aclUser = getAclUser(request);
        return Result.success(aclUser);
    }

    @Nullable
    private AclUser getAclUser(HttpServletRequest request) {
        //获取请求头
        String token = request.getHeader("token");
        //从redis中取出对象
        String s = redisTemplate.opsForValue().get("user::" + token);

        AclUser aclUser = JSON.parseObject(s, AclUser.class);
        //System.out.println("___________"+aclUser);
        return aclUser;
    }

    @PostMapping("addUserByUser")
    @ApiOperation("添加用户")
    public Result adduser(@RequestBody AddUserVo addUserVo){
        Integer addnum= userService.addUser(addUserVo);
        if (addnum>0){
            return Result.success();
        }
        return Result.fail();
    }

    @PostMapping("updateUserByUser")
    @ApiOperation("修改用户")
    public Result updateuser(@RequestBody AclUser aclUser){
       // System.out.println(aclUser);
        Integer addnum= userService.updateUser(aclUser);
        if (addnum>0){
            return Result.success();
        }
        return Result.fail();
    }




}
